package com.xique.park.service.biz.service.impl;

import com.xique.common.core.domain.R;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.park.bean.domain.ParkCar;
import com.xique.park.bean.domain.PayOrder;
import com.xique.park.bean.request.app.ParkCarAddRequest;
import com.xique.park.bean.request.app.ParkCarFeeRequest;
import com.xique.park.bean.request.web.car.ParkCarBatchFeeRequest;
import com.xique.park.service.biz.service.IPayOrderBizService;
import com.xique.park.service.service.IPayOrderService;
import com.xique.system.api.RemoteSystemProjectService;
import com.xique.system.api.dto.SystemProjectDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付记录BizService
 *
 * @author liexueli
 */
@Service
public class PayOrderBizServiceImpl implements IPayOrderBizService {

    @Autowired
    private IPayOrderService payOrderService;

    @Autowired
    private RemoteSystemProjectService remoteSystemProjectService;

    /**
     * 月卡续费
     */
    @Override
    public int generateFixedCarPayOrder(ParkCarFeeRequest request, ParkCar parkCar, Date beginDate) {
        PayOrder payOrder = new PayOrder();
        payOrder.setParkId(parkCar.getParkId());
        payOrder.setPlate(parkCar.getBindPlates());
        payOrder.setActualFee(parkCar.getActualFee());
        payOrder.setAmt(parkCar.getMayFee());
        payOrder.setPayTime(DateUtils.getNowDate());
        //1=月卡续费
        payOrder.setTransType("1");
        payOrder.setPaySsn(System.currentTimeMillis() + parkCar.getParkId() + "");
        payOrder.setPayType(request.getPayType());
        payOrder.setBeginDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, beginDate));
        payOrder.setEndDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, parkCar.getEndDate()));
        Integer months = request.getMonth();
        payOrder.setStatus("2");
        if (null != months) {
            payOrder.setMonths(0L);
            payOrder.setRemark("车牌【" + parkCar.getBindPlates() + "】续期" + months + "个月");
        } else {
            payOrder.setRemark("车牌【" + parkCar.getBindPlates() + "】续期" + DateUtils.differentDaysByMillisecond(beginDate, parkCar.getEndDate()));
        }
        payOrder.setUpdateUser(SecurityUtils.getUserId());
        payOrder.setPayChannel(getPayChannel(parkCar.getParkId()));
        return payOrderService.insertTPayOrder(payOrder);
    }

    @Override
    public int generateFixedCarPayOrder(ParkCarAddRequest request, ParkCar parkCar) {
        PayOrder payOrder = new PayOrder();
        payOrder.setParkId(parkCar.getParkId());
        payOrder.setPlate(parkCar.getBindPlates());
        payOrder.setOrderIds(parkCar.getId() + "");
        payOrder.setActualFee(parkCar.getActualFee());
        payOrder.setAmt(parkCar.getMayFee());
        payOrder.setPayTime(DateUtils.getNowDate());
        //1=月卡续费
        payOrder.setTransType("1");
        payOrder.setPayType(request.getPayType());
        if (null != parkCar.getBeginDate()) {
            payOrder.setBeginDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, parkCar.getBeginDate()));
        }
        if (null != parkCar.getEndDate()) {
            payOrder.setEndDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, parkCar.getEndDate()));
        }
        payOrder.setStatus("2");
        payOrder.setRemark("车牌【" + parkCar.getBindPlates() + "】办理月卡");
        payOrder.setCreateUser(SecurityUtils.getUserId() != null ? SecurityUtils.getUserId() : 1);
        payOrder.setUpdateUser(payOrder.getCreateUser());
        payOrder.setPayChannel(getPayChannel(parkCar.getParkId()));
        return payOrderService.insertTPayOrder(payOrder);
    }

    @Override
    public PayOrder generatePayOrderFromCar(ParkCarBatchFeeRequest request, List<ParkCar> parkCarList) {
        PayOrder payOrder = new PayOrder();
        String plateStr = parkCarList.stream().map(ParkCar::getBindPlates).collect(Collectors.joining(","));
        payOrder.setParkId(SecurityUtils.getParkId());
        payOrder.setPlate(plateStr);
        payOrder.setActualFee(request.getActualFee());
        payOrder.setAmt(request.getMayFee());
        payOrder.setPayTime(DateUtils.getNowDate());
        // 1=月卡续费
        payOrder.setTransType("1");
        payOrder.setPaySsn(System.currentTimeMillis() + SecurityUtils.getParkId() + "");
        payOrder.setPayType(request.getPayType());
        Integer months = request.getValue();
        if ("day".equals(request.getCycleType())) {
            payOrder.setMonths(months.longValue());
            payOrder.setRemark("批量续期" + months + "天");
        }
        if ("month".equals(request.getCycleType())) {
            payOrder.setMonths(months.longValue());
            payOrder.setRemark("批量续期" + months + "个月");
        }
        payOrder.setStatus("2");
        payOrder.setCreateUser(SecurityUtils.getUserId());
        payOrder.setUpdateUser(SecurityUtils.getUserId());
        payOrder.setCreateTime(new Date());
        payOrder.setUpdateTime(new Date());
        payOrder.setPayChannel(getPayChannel(SecurityUtils.getParkId()));
        return payOrder;
    }

    private String getPayChannel(Long parkId) {
        R<SystemProjectDTO> systemProjectR = remoteSystemProjectService.getProjectInfo(parkId);
        if (null != systemProjectR && null != systemProjectR.getData()) {
            SystemProjectDTO systemProject = systemProjectR.getData();
            return systemProject.getPayChannel();
        }
        return "";
    }

}
